package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw;

import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.common.OtwConstant;
import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.dto.InOutBoundOrderDeleteReq;
import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.dto.OutInWarehousePostResultReq;
import com.alibaba.cirtus.ots.application.adapter.ability.model.Result;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpTokenUtil;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpUtils;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import com.epoch.app.otsapplicationadaptersdk.mnotwif011.service.MnOtwIf011Service;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnOtwCommonResponse;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnOtwIf011Request;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 中台（sap）出入库过账结果同步otw,
 */
@Component
@Primary
public class MnOtwIf011ServiceImpl implements MnOtwIf011Service {
    private final Log log = Log.getLogger(MnOtwIf011ServiceImpl.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private HttpUtils httpUtils;

    @Override
    @ProcessInvoker(remark = "MnOtwIf011Service", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public MnOtwCommonResponse outInWarehousePostResultSync(MnOtwIf011Request mnOtwIf011Request) {
        log.info("MnOtwIf011Service outInWarehousePostResultSync param:{}", JSON.toJSONString(mnOtwIf011Request));
        MnOtwCommonResponse mnOtwCommonResponse = new MnOtwCommonResponse();
        try {
            Map<String, String> header = HttpTokenUtil.generateHttpHeader();
            String urlPrefix = OtsApplicationAdapterSdkConstants.urlPrefix();
            String url = urlPrefix + "api/mn_bc_link/outInWarehousePostResultSync";
            OutInWarehousePostResultReq outInWarehousePostResultReq = new OutInWarehousePostResultReq();
            BeanUtils.copyProperties(mnOtwIf011Request, outInWarehousePostResultReq);
            log.info("MnOtwIf011ServiceImpl outInWarehousePostResultSync, header:{}, url:{}, param:{}", header, url, JSON.toJSONString(outInWarehousePostResultReq));
            ResponseEntity<Result> post = httpUtils.post(url, header, outInWarehousePostResultReq, Result.class);
            Result result = post.getBody();
            log.info("MnOtwIf011Service outInWarehousePostResultSync mnOtwCommonResponse result:{}", JSON.toJSONString(result));
            if (result.isSuccess()) {
                mnOtwCommonResponse.setSuccess(OtwConstant.SUCCESS_TRUE);
                mnOtwCommonResponse.setResult(result.getResult());
            } else {
                mnOtwCommonResponse.setSuccess(OtwConstant.SUCCESS_FALSE);
                mnOtwCommonResponse.setErrorCode(result.getErrorCode());
                mnOtwCommonResponse.setMessage(result.getMessage());
            }
        }catch (Exception e) {
            log.error("call bc-link outInWarehousePostResultSync error", e);
            mnOtwCommonResponse.setSuccess(OtwConstant.SUCCESS_FALSE);
            mnOtwCommonResponse.setErrorCode(OtwConstant.ORDER_001_01_01_001);
            mnOtwCommonResponse.setMessage(OtwConstant.COMMON_MESSAGE);
            return mnOtwCommonResponse;
        }
        return mnOtwCommonResponse;
    }

}
